package xxx.xxxxx.template.resolver;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.module.SimpleModule;
import graphql.kickstart.servlet.apollo.ApolloScalars;
import graphql.scalars.ExtendedScalars;
import graphql.schema.GraphQLScalarType;
import graphql.schema.idl.SchemaDirectiveWiring;
import graphql.validation.rules.OnValidationErrorStrategy;
import graphql.validation.rules.ValidationRules;
import graphql.validation.schemawiring.ValidationSchemaWiring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.Part;
import java.io.IOException;

@Configuration
public class Configurer {
    @Bean
    public GraphQLScalarType dateType() {
        return ExtendedScalars.Date;
    }

    @Bean
    public GraphQLScalarType jsonType() {
        return ExtendedScalars.Json;
    }

    @Bean
    public GraphQLScalarType bigDecimalType() {
        return ExtendedScalars.GraphQLBigDecimal;
    }

    @Bean
    public GraphQLScalarType uploadType() {
        return ApolloScalars.Upload;
    }

    @Bean
    public Module partDeserializer() {
        SimpleModule module = new SimpleModule();
        module.addDeserializer(Part.class, new JsonDeserializer<Part>() {
            @Override
            public Part deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
                return null;
            }
        });
        return module;
    }

    @Bean
    public SchemaDirectiveWiring validationSchemaDirective() {
        ValidationRules validationRules = ValidationRules.newValidationRules()
                .onValidationErrorStrategy(OnValidationErrorStrategy.RETURN_NULL)
                .build();
        return new ValidationSchemaWiring(validationRules);
    }
}